<template>
  <nav class="navbar">
    <div class="navbar-container">
      <router-link to="/" class="navbar-brand">
        <h1>电脑DIY装机平台</h1>
      </router-link>
      
      <div class="navbar-menu">
        <router-link to="/" class="navbar-item">首页</router-link>
        <router-link to="/components" class="navbar-item">配件库</router-link>
        <router-link to="/builder" class="navbar-item">装机配置</router-link>
        <router-link to="/recommendations" class="navbar-item">推荐方案</router-link>
        <router-link to="/compatibility" class="navbar-item">兼容性查询</router-link>
        <router-link to="/vip" class="navbar-item vip-item">
          VIP会员
          <span v-if="isVip" class="vip-badge" :style="{ backgroundColor: vipLevelColor }"></span>
        </router-link>
      </div>
      
      <div class="navbar-user">
        <template v-if="user">
          <div class="user-info">
            <span class="username">{{ user.username }}</span>
            <span v-if="isVip" class="user-vip" :style="{ color: vipLevelColor }">{{ vipLevelText }}</span>
          </div>
          <button @click="handleLogout" class="btn-secondary">退出</button>
        </template>
        <template v-else>
          <router-link to="/auth?mode=login" class="btn-primary">登录</router-link>
          <router-link to="/auth?mode=register" class="btn-secondary">注册</router-link>
        </template>
      </div>
    </div>
  </nav>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import type { User, VipInfo } from '../types/vip';
import { getUserWithVipInfo, isVipActive, getVipLevel, formatVipLevel, getVipLevelColor } from '../utils/vipService';

const router = useRouter();
const user = ref<User | null>(null);

// 计算属性
  const isVip = computed(() => isVipActive(user.value?.vipInfo));
  const vipLevel = computed(() => getVipLevel(user.value?.vipInfo));
const vipLevelText = computed(() => formatVipLevel(vipLevel.value));
const vipLevelColor = computed(() => getVipLevelColor(vipLevel.value));

// 处理登出
const handleLogout = () => {
  localStorage.removeItem('user');
  user.value = null;
  router.push('/');
};

// 加载用户信息
const loadUserInfo = () => {
  user.value = getUserWithVipInfo();
};

// 组件挂载时加载用户信息
onMounted(() => {
  loadUserInfo();
  
  // 监听storage事件，以便在其他标签页登录/登出时更新
  window.addEventListener('storage', (e) => {
    if (e.key === 'user') {
      loadUserInfo();
    }
  });
});
</script>

<style scoped>
.navbar {
  background-color: #1a1a1a;
  color: white;
  padding: 1rem 0;
  position: sticky;
  top: 0;
  z-index: 1000;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

.navbar-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1.5rem;
}

.navbar-brand h1 {
  margin: 0;
  font-size: 1.8rem;
  color: #00ccff;
  font-weight: 700;
}

.navbar-menu {
  display: flex;
  gap: 2rem;
}

.navbar-item {
  color: white;
  text-decoration: none;
  font-size: 1.1rem;
  font-weight: 500;
  transition: color 0.3s ease;
  padding: 0.5rem 0;
  position: relative;
}

.navbar-item:hover {
  color: #00ccff;
}

.navbar-item::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 2px;
  background-color: #00ccff;
  transition: width 0.3s ease;
}

.navbar-item:hover::after {
  width: 100%;
}

.navbar-user {
  display: flex;
  gap: 1rem;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.username {
  color: white;
  font-weight: 500;
}

.user-vip {
  font-size: 0.85rem;
  font-weight: bold;
  padding: 0.25rem 0.5rem;
  border-radius: 10px;
  background-color: rgba(255, 255, 255, 0.1);
}

.vip-item {
  position: relative;
}

.vip-badge {
  position: absolute;
  top: 0;
  right: -8px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.btn-primary, .btn-secondary {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.btn-primary {
  background-color: #00ccff;
  color: #1a1a1a;
}

.btn-primary:hover {
  background-color: #00a3cc;
  transform: translateY(-2px);
}

.btn-secondary {
  background-color: transparent;
  color: white;
  border: 2px solid white;
}

.btn-secondary:hover {
  background-color: white;
  color: #1a1a1a;
  transform: translateY(-2px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .navbar-container {
    flex-direction: column;
    gap: 1rem;
  }
  
  .navbar-menu {
    flex-wrap: wrap;
    justify-content: center;
    gap: 1rem;
  }
  
  .navbar-brand h1 {
    font-size: 1.5rem;
  }
}
</style>